<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="province">
        <el-select
          v-model="queryParams.provinceId"
          placeholder="全部省份"
          clearable
        >
          <el-option
            v-for="item in provinces"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="years">
        <el-select
          v-model="queryParams.years"
          placeholder="全部年份"
          clearable
        >
          <el-option
            v-for="item in years"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="cardType">
        <el-select
          v-model="queryParams.cardType"
          placeholder="全部类型"
          clearable
        >
          <el-option
            v-for="item in cardType"
            :key="item.id"
            :label="item.cardType"
            :value="item.cardType"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['expert:expert:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['expert:expert:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['expert:expert:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['expert:expert:export']"
        >导出
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" fixed/>
      <el-table-column label="卡类型" align="center" prop="cardType"/>
      <el-table-column label="简介" align="center" prop="cardDesc"/>
      <el-table-column label="卡图" align="center" prop="cardImg">
        <template #default="scope">
          <img :src="scope.row.cardImg" alt="图片" style="max-width: 120px; max-height: 70px;object-fit: contain">
        </template>
      </el-table-column>
      <el-table-column label="内部价" align="center" prop="internalPrice"/>
      <el-table-column label="原价" align="center" prop="originalPrice"/>
      <el-table-column label="省份" align="center" prop="province"/>
      <el-table-column label="秒杀价" align="center" prop="seckillPrice"/>
      <el-table-column label="年份" align="center" prop="years"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="省份" style="width: 300px" prop="province">
          <el-select
            v-model="form.provinceId"
            placeholder="全部省份"
            clearable
          >
            <el-option
              v-for="item in provinces"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年份" style="width: 300px" prop="years">
          <el-select
            v-model="form.years"
            placeholder="全部年份"
            clearable
          >
            <el-option
              v-for="item in years"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="卡类型" prop="cardType" required>
          <el-select
            v-model="form.cardType"
            placeholder="全部类型"
            clearable
          >
            <el-option
              v-for="item in cardType"
              :key="item.id"
              :label="item.cardType"
              :value="item.cardType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学科数" v-if="form.cardType === '提分卡'" prop="subjectNum" required>
          <el-input-number v-model="form.subjectNum"
                           :min="1"
                           default-value="1"
                           :max="6"
                           style="width: 200px; margin-top: 5px;"/>
        </el-form-item>
        <el-button type="primary" @click="dialogVisible = true" style="float: right;margin-right: 90px">增加卡类型
        </el-button>
        <el-form-item label="卡用途" prop="cardUseTo" required>
          <el-select v-model="form.cardUseTo" placeholder="请选择">
            <el-option label="正式卡" value="正式卡"/>
            <el-option label="体验卡" value="体验卡" v-if="form.cardType !== 3"/>
          </el-select>
        </el-form-item>
        <el-form-item label="内部价" style="width: 300px" prop="internalPrice">
          <el-input v-model="form.internalPrice"/>
        </el-form-item>
        <el-form-item label="原价" style="width: 300px" prop="originalPrice">
          <el-input v-model="form.originalPrice"/>
        </el-form-item>
        <el-form-item label="秒杀价" style="width: 300px" prop="seckillPrice">
          <el-input v-model="form.seckillPrice"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 弹窗模板 -->
    <el-dialog title="增加卡类型" :visible.sync="dialogVisible" width="30%">
      <el-form :model="newCardTypeForm" label-width="80px">
        <el-form-item label="卡类型">
          <el-input v-model="newCardTypeForm.cardType"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCardType">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
// 查询参数
import {getUserProvince} from "@/api/expert/type";
import {addExpert, delExpert, getExpert, updateExpert} from "@/api/expert/expert";
import {getAllCardType} from "@/api/system/purchase";
import {getCardTypes} from "@/api/expert/records";

export default {
  name: "cardPurchase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 卡类型表格数据
      typeList: [],
      cardType: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      //省份
      provinces: [],
      years: {},
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardType: null,
        years: null,
        provinceId: null,
      },
      newCardTypeForm: {
        cardType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getUserProvince();
    this.getList();
    this.getCardTypes();
    this.getYears();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      getAllCardType(this.queryParams).then(res => {
        this.typeList = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    getCardTypes() {
      getCardTypes().then(res => {
        this.cardType = res.data;
        this.loading = false;
      })
    },
    //获取从今年开始五年的年份
    getYears() {
      const currentYear = new Date().getFullYear();
      this.years = [];
      for (let i = 0; i <= 4; i++) {
        const year = currentYear + i;
        this.years.push({
          value: year,
          label: year.toString()
        });
      }
    },
    addCardType() {

    },
    // 表单重置
    reset() {
      this.form = {
        provinceId: null,
        years: null,
        cardType: null,
        cardDesc: null,
        cardImg: null,
        internalPrice: null,
        originalPrice: null,
        seckillPrice: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卡用途";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      // getExpert(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改卡用途";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExpert(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除卡类型编号为"' + ids + '"的数据项？').then(function () {
        return delExpert(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('expert/expert/export', {
    //     ...this.queryParams
    //   }, `expert_${new Date().getTime()}.xlsx`)
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //查询省份
    getUserProvince() {
      getUserProvince().then(response => {
        if (Array.isArray(response.data)) {
          response.data.forEach(item => {
            this.provinces.push({
              value: item.id,
              label: item.province
            });
          });
        } else {
          // 如果 response.data 不是数组，直接添加
          this.provinces.push({
            value: response.data.id,
            label: response.data.province
          });
        }
      });
    },
  }
}
</script>

<style scoped lang="scss">

</style>
